package com.fengye.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fengye.api.mapper.CommentMapper;
import com.fengye.common.constants.CommonConstant;
import com.fengye.common.utils.BeanCopyUtils;
import com.fengye.common.domain.MyPage;
import com.fengye.common.domain.entity.Comment;
import com.fengye.common.domain.entity.User;
import com.fengye.common.domain.vo.CommentVo;
import com.fengye.api.service.CommentService;
import com.fengye.security.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author sunlei
* @description 针对表【fy_comment】的数据库操作Service实现
* @createDate 2024-02-28 23:02:35
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService {

    @Autowired
    private UserService userService;

    @Override
    public MyPage<CommentVo> getCommentList(Long articleId, Integer pageNum, Integer pageSize) {
        // 查询文章的根评论
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        // 对articleId进行判断
        queryWrapper.eq(Comment::getArticleId, articleId);
        // 根评论rootId为-1
        queryWrapper.eq(Comment::getRootId, CommonConstant.COMMENT_ROOT_ID_BLANK);

        // 分页查询根评论
        Page<Comment> page = new Page<>(pageNum, pageSize);
        Page<Comment> commentPage = page(page, queryWrapper);

        List<CommentVo> commentVoList = toCommentVoList(page.getRecords());

        // 查询跟评论下的子评论
        List<CommentVo> commentVos = commentVoList.stream().peek(commentVo -> {
            commentVo.setChildren(getChildrenCommentList(commentVo.getId()));
        }).collect(Collectors.toList());


        return new MyPage<CommentVo>(commentVos, commentPage.getTotal(), commentPage.getCurrent(), commentPage.getSize());
    }

    @Override
    public void addComment(Comment comment) {
        save(comment);
    }

    /**
     * 查询子评论
     * @param id 父评论id
     * @return 子评论列表
     */
    private List<CommentVo> getChildrenCommentList(Long id) {
        // 查询子评论
        LambdaQueryWrapper<Comment> subQueryWrapper = new LambdaQueryWrapper<>();
        // 对articleId进行判断
        subQueryWrapper.eq(Comment::getRootId, id);
        // 根据评论时间升序
        subQueryWrapper.orderByAsc(Comment::getCreateTime);

        List<Comment> subCommentList = list(subQueryWrapper);

        return toCommentVoList(subCommentList);
    }

    /**
     * 将模型数据转换为对应的vo
     * @param list 模型列表
     * @return vo列表
     */
    private List<CommentVo> toCommentVoList(List<Comment> list) {
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(list, CommentVo.class);

        return commentVos.stream().peek(commentVo -> {
            User user = userService.getById(commentVo.getCreateBy());
            commentVo.setUserName(user.getNickname());
            if (commentVo.getToCommentId() != -1) {
                User user1 = userService.getById(commentVo.getToCommentUserId());
                commentVo.setToCommentUserName(user1.getNickname());
            }
        }).collect(Collectors.toList());
    }
}




